import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductManagementActivity extends Activity {

    private EditText productNameEditText;
    private EditText productPriceEditText;
    private Button addProductButton;
    private Button editProductButton;
    private Button deleteProductButton;
    private ListView productListView;

    private List<Map<String, Object>> productDataList;
    private int currentSelectedIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_management);

        productNameEditText = findViewById(R.id.product_name_edittext);
        productPriceEditText = findViewById(R.id.product_price_edittext);
        addProductButton = findViewById(R.id.add_product_button);
        editProductButton = findViewById(R.id.edit_product_button);
        deleteProductButton = findViewById(R.id.delete_product_button);
        productListView = findViewById(R.id.product_listview);

        productDataList = new ArrayList<>();

        addProductButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String productName = productNameEditText.getText().toString();
                String productPrice = productPriceEditText.getText().toString();

                Map<String, Object> productMap = new HashMap<>();
                productMap.put("name", productName);
                productMap.put("price", productPrice);

                productDataList.add(productMap);

                refreshListView();

                productNameEditText.setText("");
                productPriceEditText.setText("");
            }
        });

        editProductButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentSelectedIndex!= -1) {
                    Map<String, Object> selectedProduct = productDataList.get(currentSelectedIndex);

                    selectedProduct.put("name", productNameEditText.getText().toString());
                    selectedProduct.put("price", productPriceEditText.getText().toString());

                    refreshListView();
                }
            }
        });

        deleteProductButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentSelectedIndex!= -1) {
                    productDataList.remove(currentSelectedIndex);
                    currentSelectedIndex = -1;

                    refreshListView();
                }
            }
        });

        productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentSelectedIndex = position;

                Map<String, Object> selectedProduct = productDataList.get(position);

                productNameEditText.setText(selectedProduct.get("name").toString());
                productPriceEditText.setText(selectedProduct.get("price").toString());
            }
        });
    }

    private void refreshListView() {
        SimpleAdapter adapter = new SimpleAdapter(this, productDataList, R.layout.product_item,
                new String[]{"name", "price"},
                new int[]{R.id.product_name_item, R.id.product_price_item});

        productListView.setAdapter(adapter);
    }
}